Contents
Full Software
Trial Software
News Plus
Online
Hands On
Essential Files
Contact Details
Help Section
Contents Page


Delphi Workshop

Huw Collingbourne explains the basic techniques needed to create a multi-threaded application using Delphi 2 or 3.

Many commercial Windows 95 applications are multi-threaded. That means they create multiple æexecution threadsÆ in which different parts of the program can run more or less simultaneously. Note however, that multi-threaded programming comes with a health warning:

If you arenÆt a fairly experienced programmer, donÆt use threads!

But what exactly is a thread? Well, every 32-bit application comprises a æprocessÆ which defines a block of memory sealed off from other applications. This process contains at least one thread (the æprimary threadÆ) which runs the code itself.

First, letÆs look at a simple application which, like all normal Delphi programs, contains just the primary thread. This program increments and displays numbers from 0 to 2000. When you click the Stop button it sets a Boolean value that tries to stop the numbers from being incremented further.


Run RUNSTOP.EXE and try to stop the display on the number 200. YouÆll find it wonÆt work! This is because the ButtonClick event is trapped inside the same thread of execution as the loop-counter itself and it wonÆt be processed until after the loop has completed.

Now run THRUNSTOP.EXE and once again, try to stop on 200. This time it will work. ThatÆs because this program has created a separate thread which runs alongside the primary thread. Now the ButtonClick event is processed quickly, so that the Boolean value is changed and the loop stops at the desired point.

Of course, in many circumstances, you may want some threads to be given more processor time than others. For example, if you were writing a spreadsheet that did automatic backups every five minutes, you would probably want to make sure that the thread containing the backups operated æin the backgroundÆ. You wouldnÆt want the file-saving to slow down the other spreadsheet operations.

To make fine adjustments of this sort, you can set thread priorities. Our final program, THPRIORITIES.EXE, uses the Windows APIÆs SetThreadPriority() function to control the amount of time allocated to the first of two racing scroll-bars.

Set your priorities and place your bets!


To Copy all the delphi tutorial files to your disk.




 
 
Contents | Top | Help